package m2_work;

import com.rabbitmq.client.*;
import org.apache.log4j.net.SyslogAppender;
import sun.rmi.transport.proxy.CGIHandler;

import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("192.168.126.130");
        cf.setUsername("admin");
        cf.setPassword("admin");
        //创建通道
        Channel channel = cf.newConnection().createChannel();
        //定义队列
        channel.queueDeclare("task-queue", true, false, false, null);
        DeliverCallback deliverCallback = new DeliverCallback() {

            @Override
            public void handle(String s, Delivery message) throws IOException {
                String msg = new String(message.getBody());
                System.out.println("收到"+msg);
                for (int i=0 ; i<msg.length(); i++) {
                    if (msg.charAt(i)=='.') {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
                System.out.println("消息处理完成\n");
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
            channel.basicQos(1);
        //消费数据
        System.out.println("开始");
        channel.basicConsume("task-queue", false, deliverCallback, cancelCallback);
    }
}
